<!DOCTYPE html>
<html>
<title>测试PC浏览器定位</title>
<body>
<p id="demo">点击这个按钮，通过H5获得您的坐标：</p>
<div id="allmap"></div>
<button onclick="getLocation()">H5测试</button>

<!--<script src="../../js/lib/jquery/jquery.min.js"></script>-->
<!--<script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>-->
<!--<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>-->
<script src="https://s3.pstatp.com/cdn/expire-1-M/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript"
        src="http://api.map.baidu.com/api?v=3.0&ak=YE1USOyvQqtaXRjoZmjl7EUwar8ib1FG"></script>
<script>

  /* H5定位方法 */
  var x = document.getElementById("demo");
  function getLocation() {
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(showPosition,
        showError);
    } else {
      console.log("请求失败")
    }
  }
  function showPosition(position) {
    console.log(position.coords.latitude + "   "
      + position.coords.longitude)
  }
  function showError(error) {
    console.log("H5错误编码  " + error.code)

    switch (error.code) {
      case error.PERMISSION_DENIED:
        x.innerHTML = "User denied the request for Geolocation."
        break;
      case error.POSITION_UNAVAILABLE:
        x.innerHTML = "Location information is unavailable."
        break;
      case error.TIMEOUT:
        x.innerHTML = "The request to get user location timed out."
        break;
      case error.UNKNOWN_ERROR:
        x.innerHTML = "An unknown error occurred."
        break;
    }
  }

  /* 百度api 通过浏览器定位 */
  var geolocation1 = new BMap.Geolocation();
  geolocation1.getCurrentPosition(function(r) {
    console.log('通过浏览器定位 getStatus()', this.getStatus())
    if (this.getStatus() == BMAP_STATUS_SUCCESS) {
      var mk = new BMap.Marker(r.point);
      alert('浏览器定位您的位置：' + r.point.lng + ',' + r.point.lat);
    } else {
      alert('浏览器定位 failed 状态  ' + this.getStatus());
    }
  });

  //百度api 通过ip定位
  function myFun(result) {
    var cityName = result.name;
    alert("IP 定位城市:" + cityName+"   经度"+result.center.lng+"   纬度"+result.center.lat);
  }
  var myCity = new BMap.LocalCity();
  myCity.get(myFun);

  //百度api SDK辅助定位
  var geolocation = new BMap.Geolocation();
  // 开启SDK辅助定位
  geolocation.enableSDKLocation();
  geolocation.getCurrentPosition(function(r) {
    console.log('SDK辅助定位 getStatus()', this.getStatus())
    if (this.getStatus() == BMAP_STATUS_SUCCESS) {
      var mk = new BMap.Marker(r.point);
      alert('SDK辅助定位 您的位置：' + r.point.lng + ',' + r.point.lat);
    } else {
      alert('SDK辅助failed  状态' + this.getStatus());
    }
  });
</script>
</body>

</html>
